<?php
if($_REQUEST["oldphoto"]!=" "&&$_FILES["photo"]["error"]>0){
    $newname = $_REQUEST["oldphoto"];
}else{
    if($_FILES["photo"]["error"]>0){
        echo "上传不成功".$_FILES["photo"]["error"];
    }else{
        // echo "上传文件的名称".$_FILES["photo"]["name"];
        // echo "上传文件的类型".$_FILES["photo"]["type"];
        // echo "上传文件的大小".$_FILES["photo"]["size"];
        // echo "临时文件的路径".$_FILES["photo"]["tmp_name"];
        function randStr($i){
            $str = "abcdefghijklmnopqrstuvwxyz";
            $finalStr = "";
            for($j=0;$j<$i;$j++)
            {
            $finalStr .= substr($str,rand(0,25),1);
            // echo "$finalStr";
            }
            return $finalStr;
            }
        $str1 = randStr(10);
        if(($_FILES["photo"]["type"]=="image/gif") || ($_FILES["photo"]["type"]=="image/jpeg") ||($_FILES["photo"]["type"]=="image/pjpeg") && ($_FILES["photo"]["size"]<9097152)){
            //生成一个新的文件文件名
            // $newname = __DIR__."/upload/".date("YmdHis").substr($_FILES["photo"]["name"],-4,4);
            $newname = "upload/".date("YmdHis").$str1.substr($_FILES["photo"]["name"],-4,4);
            echo $newname;
            $filename = __DIR__."/".$newname;
            echo "<br>";
            // die($newname);
            //参数local:临时文件的路径
            //参数path：最终存放路径  
            move_uploaded_file($_FILES["photo"]["tmp_name"],$filename);
        }else{
            echo "上传的文件格式或大小不符合要求";
        }
    }
}
$sids = $_REQUEST["id"];
$xuehao = $_REQUEST["xuehao"];
$name = $_REQUEST["names"];
$sex = $_REQUEST["sex"];
$birthday = $_REQUEST["birthday"];
$jiguan = $_REQUEST["jiguan"];
$iphone = $_REQUEST["iphone"];
$banhao = $_REQUEST["banhao"];
// $photo = $_REQUEST["photo"];
$action = $_REQUEST["action"];
// echo "$sids";
echo "$xuehao";
echo "$name";
echo "$jiguan";
echo "$iphone";
echo "$banhao";

include_once("conn.php");
//3.运行sql语句，执行增、删、改、查
if($action=="add"){
    $sql1 = "insert into `student`(学号,姓名,性别,生日,籍贯,手机号,班号,照片)value('{$xuehao}','{$name}','{$sex}','{$birthday}','{$jiguan}','{$iphone}','{$banhao}','{$newname}')";
    $str = "数据插入成功";
}else if($action=="update"){
    $sql1 = "update student set 学号='{$xuehao}',姓名='{$name}',性别='{$sex}',生日='{$birthday}',籍贯='{$jiguan}',手机号='{$iphone}',班号='{$banhao}',照片='{$newname}' where sid='{$sids}'";
    $str = "数据修改成功";
}else{
    die("请正确输入参数");
}

// $sql1 = "insert into `student`(学号,姓名,籍贯,手机号,班号)value('{$xuehao}','{$name}','{$jiguan}','{$iphone}','{$banhao}')";
$result = mysqli_query($conn,$sql1);
if($result){
    echo "<script>alert('$str')</script>";
    header("Refresh:1;url=xuesheng-list.php");
}else{
    echo "<script>alert('数据插入失败')</script>";
}
//4.关闭连接，释放资源
mysqli_close($conn);

?>